/**
 * Copyright (c) 2005-2009 springside.org.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * 
 * $Id: SpringContextUtils.java 422 2009-09-05 14:43:11Z calvinxiu $
 */
package com.pignest.hotel.spring;

import org.springframework.context.ApplicationContext;

public class ContextHolder {

	/** spring 上下文 */
	private static ApplicationContext applicationContext;
//	private static ContextLoader contextLoader;
	//上下文路径
	private static String contextPath;
	//web根目录在系统中的真实路径
	private static String realPath;
	
	public static void setApplicationContext(ApplicationContext context) {
		ContextHolder.applicationContext = context;
	}
	
//	static void setContextLoader(ContextLoader contextLoader) {
//		ContextHolder.contextLoader = contextLoader;
//	}
//	
//	public static ContextLoader getContextLoader() {
//		return contextLoader;
//	}
	
	static void setContextPath(String contextPath) {
		ContextHolder.contextPath = contextPath;
	}
	
	public static String getContextPath() {
		return contextPath;
	}
	
	public static String getRealPath() {
		return realPath;
	}
	
	static void setRealPath(String realPath) {
		ContextHolder.realPath = realPath;
	}

	/**
	 * 取得存储在静态变量中的ApplicationContext.
	 */
	public static ApplicationContext getApplicationContext() {
		if (applicationContext == null)
			throw new IllegalStateException("applicaitonContext未注入");
		return applicationContext;
	}

	/**
	 * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getBean(String name) {
		return (T) applicationContext.getBean(name);
	}
	
}
